//
//  MakeSureTheOrderViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/11.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MakeSureTheOrderViewController.h"
#import "MethodOfPaymentView.h"
#import "TheMembershipCardToPayView.h"
#import "LackOfBalanceView.h"
#import "MemberPrepaidPhoneViewController.h"
#import "MakeSureTheOrderOneTableViewCell.h"
#import "MakeSureTheOrderSecondTableViewCell.h"
#import "MyReceiveAddressViewController.h"

@interface MakeSureTheOrderViewController ()<UITableViewDelegate , UITableViewDataSource , UITextViewDelegate>
{
    NSString *promptStrong;
    NSDictionary *model;
}
Strong UIView *headView;
Strong UITableView *tableView;
Strong UIView *footView;
Strong UIButton *paymentButton; /**<*/
Strong UITextView *textView;
Strong NSMutableArray *dataArray; /**<*/
Strong UILabel *phoneNumberLabel;
Strong UILabel *moneyLabel;
Strong NSMutableArray *contentArray; /**<*/

@end

//网络
@implementation MakeSureTheOrderViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"soId":self.so_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Order/viewOrderDetails.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            model = responseObject[@"obj"];
            self.contentArray = responseObject[@"obj2"];
            [self.view addSubview:self.tableView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation MakeSureTheOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"确认订单";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - click

- (void)setSo_id:(NSString *)so_id{
    _so_id = so_id;
    
    promptStrong = @"请输入留言内容";
    [self getData];
}

- (void)tableHeadViewCkick{
    [self callPhone:self.phoneNumberLabel.text];
}

- (void)paymentButtonClick{
    MethodOfPaymentView *view = [[MethodOfPaymentView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) selectedHandle:^(NSString *str) {
        switch ([str integerValue]) {
            case 0:
                NSLog(@"支付宝支付");
                break;
            case 1:
                NSLog(@"微信支付");
                break;
            case 2:
                NSLog(@"会员卡支付");
                //                if ([[UserModel sharedInstanced].payThePassword isEqualToString:@""]) {
                //                    ShowMessage(@"请先设置支付密码");
                //                    return ;
                //                }
                TheMembershipCardToPayView *view = [[TheMembershipCardToPayView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) selectedHandle:^(NSString * str){
                    NSLog(@"输入密码%@",str);
                    LackOfBalanceView *view = [[LackOfBalanceView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) selectedHandle:^{
                        NSLog(@"余额不足");
                        MemberPrepaidPhoneViewController *vc = [MemberPrepaidPhoneViewController new];
                        [self.navigationController pushViewController:vc animated:YES];
                    }];
                    [view show];
                }];
                [view show];
                break;
        }
    }];
    [view show];
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.contentArray.count;
    }
    return 3 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MakeSureTheOrderOneTableViewCell *cell = [MakeSureTheOrderOneTableViewCell cellForTableView:tableView];
        cell.model = self.contentArray[indexPath.row];
        //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
        return cell;
    }
    MakeSureTheOrderSecondTableViewCell *cell = [MakeSureTheOrderSecondTableViewCell cellForTableView:tableView];
    cell.indexPath = indexPath;
    cell.model = self.dataArray[indexPath.row];
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 33 / WIDTH_5S_SCALE;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 33 / WIDTH_5S_SCALE)];
    UILabel *allMoneyLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, 200 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
    allMoneyLabel.font = DEF_FontSize_14;
    allMoneyLabel.textColor = getColor(mainColor);
    allMoneyLabel.text = @"总价";
    [view sd_addSubviews:@[allMoneyLabel , self.moneyLabel]];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 && indexPath.row == 1) {
        MyReceiveAddressViewController *vc = [MyReceiveAddressViewController new];
        vc.getAddress = ^(NSString *str) {
            self.dataArray[indexPath.row][@"content"] = str;
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 65 / WIDTH_5S_SCALE;
    }
    return 85 / WIDTH_5S_SCALE;
}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:promptStrong]) {
        textView.text = @"";
        textView.textColor = getColor(textColor);
    }
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        textView.text = promptStrong;
        textView.textColor = getColor(@"999999");
    }
    
    return YES;
}


#pragma mark - init

- (UITextView *)textView{
    if (!_textView) {
        _textView  = [[UITextView alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE + 1 + 5 / WIDTH_5S_SCALE, SCREEN_WIDTH - 20 / WIDTH_5S_SCALE, 69 / WIDTH_5S_SCALE)];
        _textView.font = DEF_FontSize_14;
        _textView.textColor = getColor(@"999999");
        _textView.delegate = self;
        _textView.text = promptStrong;
    }
    return _textView;
}

- (UILabel *)phoneNumberLabel{
    if (!_phoneNumberLabel) {
        _phoneNumberLabel  = [[UILabel alloc]initWithFrame:CGRectMake(100 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH - 110 / WIDTH_5S_SCALE, 30 / WIDTH_5S_SCALE)];
        _phoneNumberLabel.font = DEF_FontSize_14;
        _phoneNumberLabel.textColor = getColor(mainColor);
        _phoneNumberLabel.textAlignment = NSTextAlignmentRight;
        _phoneNumberLabel.text = model[@"s_mobile"];
    }
    return _phoneNumberLabel;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30 / WIDTH_5S_SCALE)];
        UILabel *leftLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, 200, 30 / WIDTH_5S_SCALE)];
        leftLabel.font = DEF_FontSize_14;
        leftLabel.textColor = getColor(mainColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, - 2, 10 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"phone"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@" 联系商家"]];
        leftLabel.attributedText = attStr1;
        [_headView addSubview:leftLabel];
        [_headView addSubview:self.phoneNumberLabel];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 30 / WIDTH_5S_SCALE - 0.5, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        [_headView addSubview:lineView];
        _headView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableHeadViewCkick)];
        [_headView addGestureRecognizer:tap];
    }
    return _headView;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300 / WIDTH_5S_SCALE - 33 / WIDTH_5S_SCALE)];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        [_footView addSubview:lineView];
        
        UILabel *leftLabel  = [[UILabel alloc]initWithFrame:CGRectMake(5 / WIDTH_5S_SCALE, 0, 200 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
        leftLabel.font = DEF_FontSize_14;
        leftLabel.textColor = getColor(mainColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, - 2, 10 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"root_IWantToLeaveAMessage"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@" 我要留言"]];
        leftLabel.attributedText = attStr1;
        [_footView addSubview:leftLabel];
        
        UIView *line1View = [[UIView alloc]initWithFrame:CGRectMake(0, 33 / WIDTH_5S_SCALE - 0.5, SCREEN_WIDTH, 0.5)];
        line1View.backgroundColor = getColor(dividerColor);
        [_footView addSubview:line1View];
        [_footView addSubview:self.textView];
        
        UIView *line2View = [[UIView alloc]initWithFrame:CGRectMake(0, 33 / WIDTH_5S_SCALE  + 1 + 5 / WIDTH_5S_SCALE + 69 / WIDTH_5S_SCALE, SCREEN_WIDTH, 0.5)];
        line2View.backgroundColor = getColor(dividerColor);
        [_footView addSubview:line2View];
        [_footView addSubview:self.paymentButton];
    }
    return _footView;
}

- (UIButton *)paymentButton{
    if (!_paymentButton) {
        _paymentButton = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 106 / WIDTH_5S_SCALE) / 2, 33 / WIDTH_5S_SCALE  + 1 + 5 / WIDTH_5S_SCALE + 69 / WIDTH_5S_SCALE + 50 / WIDTH_5S_SCALE, 106 / WIDTH_5S_SCALE, 22 / WIDTH_5S_SCALE)];
        _paymentButton.titleLabel.font = DEF_FontSize_14;
        _paymentButton.layer.masksToBounds = YES;
        _paymentButton.layer.cornerRadius = 11 / WIDTH_5S_SCALE;
        [_paymentButton setTitle:@"付款" forState:UIControlStateNormal];
        [_paymentButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        _paymentButton.backgroundColor = getColor(mainColor);
        [_paymentButton addTarget:self action:@selector(paymentButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _paymentButton;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel  = [[UILabel alloc]initWithFrame:CGRectMake(100 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH - 110 / WIDTH_5S_SCALE, 33 / WIDTH_5S_SCALE)];
        _moneyLabel.font = DEF_FontSize_14;
        _moneyLabel.textColor = getColor(mainColor);
        _moneyLabel.text = [NSString stringWithFormat:@"¥%.2f",[model[@"so_total"] floatValue]];
        _moneyLabel.textAlignment = NSTextAlignmentRight;
    }
    return _moneyLabel;
}

#pragma mark - data

- (NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray = @[].mutableCopy;
    }
    return _contentArray;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[@{@"image":@"root_TheOrderNumber",@"title":@"订单号",@"content":[model[@"order_time"] substringToIndex:19],@"theOrderNumber":model[@"so_number"]}.mutableCopy,
                        @{@"image":@"ShippingAddress",@"title":@"收获地址",@"content":model[@"delivery_address"]}.mutableCopy,
                        @{@"image":@"ShopIsIntroduced",@"title":@"商品描述",@"content":self.contentArray[0][@"c_introduce"]}.mutableCopy,
                        
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
